package servlet;

import com.alibaba.fastjson.JSON;
import domain.*;
import dao.DAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type=request.getParameter("type");
        if("findAll_order".equals(type)){
            getAllOrders(request,response);
        }else if ("receiveOrder".equals(type)){
            receiveOrder(request,response);
        }else if("evaluate".equals(type)){
            orderEvaluate(request,response);
        }else if("query_order".equals(type)){
            queryOrder(request,response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    DAO dao = new DAO();

    public void getAllOrders(HttpServletRequest request, HttpServletResponse response) {

        List<Order> orderList = dao.getAllOrders();
        String listStr = JSON.toJSONString(orderList);

        try {
            response.getWriter().print(listStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void queryOrder(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        List<Order> list = dao.findOrderById(id);
        //3.把数据结果返回客户端
        String listStr = JSON.toJSONString(list);

        try {
            response.getWriter().print(listStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void receiveOrder(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        int result = 0;
        result = dao.orderReceive(id);
        response.setCharacterEncoding("utf-8");
        PrintWriter out= null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(result == 1){
            out.print("收货成功!");
        }else{
            out.print("收货失败！");
        }

        out.flush();
        out.close();
    }
    public void orderEvaluate(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        String rank = request.getParameter("rank");

        int result = 0;
        result = dao.orderEvaluate(id,rank);
        response.setCharacterEncoding("utf-8");
        PrintWriter out= null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(result == 1){
            out.print("评价成功!");
        }else{
            out.print("评价失败，请重试！");
        }

        out.flush();
        out.close();

    }
}
